package com.borqsprocessmanager.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;

public class MyButton extends Button {
    private final String TAG = "MyButton";

    private ColorStateList csl;
    Drawable mdDrawable;
    // private String[] colors = { "#FF3300", "#ECECEC" };
    private String[] colors = { "#FFFFFF" };
    private Paint buttonPaint;
    private int width;
    private int height;

    private boolean isDraw = true;
    private boolean istouch = false;

    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyButton(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        csl = getTextColors();
        buttonPaint = new Paint();

        buttonPaint.setAntiAlias(true);
        buttonPaint.setColor(csl.getDefaultColor());
        buttonPaint.setStrokeWidth(5);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        // TODO Auto-generated method stub
        super.onLayout(changed, left, top, right, bottom);
        if (width == 0) {
            width = getWidth();
            height = getHeight();
            Log.v(TAG, "width===" + width);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isDraw) {
            if (!istouch) {
                Log.v(TAG, "if=" + istouch);
                setBackgroundColor(csl.getDefaultColor());
                setTextColor(Color.parseColor(colors[0]));
            } else {
                Log.v(TAG, "else=" + istouch);
                canvas.drawLine(0, 0, width, 0, buttonPaint);
                canvas.drawLine(0, 0, 0, height, buttonPaint);
                canvas.drawLine(width, 0, width, height, buttonPaint);
                canvas.drawLine(0, height, width, height, buttonPaint);
                setBackgroundColor(Color.parseColor("#00FFFFFF"));
                setTextColor(csl);
            }
            isDraw = false;
        }
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.v("xxx", "ACTION_DOWN");
            istouch = true;
            isDraw = true;
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            Log.v("xxx", "ACTION_UP");
            istouch = false;
            isDraw = true;
            invalidate();
            break;
        default:
            break;
        }
        return true;
    }

    public void setBackgroudColor(String color) {
        colors[0] = color;
    }

}
